package BUS;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {
	private Socket _clientSocket;
	private ObjectInputStream _ois;
	private ObjectOutputStream _oos;
	private boolean _isConnected = false;

	public void ConnectTo(String IP, int Port) {
		try {
			_clientSocket = new Socket(IP, Port);
			_ois = new ObjectInputStream(_clientSocket.getInputStream());
			_oos = new ObjectOutputStream(_clientSocket.getOutputStream());
			_isConnected = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			_isConnected = false;
			e.printStackTrace();
		}
	}

	public void SendCommand(CommandFromClient command) {
		try {
			if (_oos != null)
				_oos.writeObject(command);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public DataPackageFromServer RecieveCommand() {
		DataPackageFromServer result = null;
		try {
			if (_ois != null)
				result = (DataPackageFromServer) _ois.readObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean isConneccted() {
		// TODO Auto-generated method stub
		return _isConnected;
	}

	public void disconnect() {
		// TODO Auto-generated method stub
		_isConnected = false;
	}
}
